package com.thiswhyme.easyexcel.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.thiswhyme.common.expection.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @description: excel工具
 * @author: thiswhyme
 * @date: 2021/9/15
 */
@Slf4j
public class ExcelUtils {

    /**
     * 读取数据
     * @param in
     * @param dataClass
     * @return
     */
    public static <T> List<T> read(InputStream in, Class<T> dataClass) {
        DefaultDataGetListener<T> listener = new DefaultDataGetListener<>();
        EasyExcel.read(in, dataClass, listener).sheet().doRead();
        return listener.getData();
    }

    /**
     * 写入数据到response
     * @param response
     * @param fileName
     * @param dataClass
     * @param data
     * @throws IOException
     */
    public static <T> void write(HttpServletResponse response, String fileName, Class<T> dataClass, List<T> data) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);

        try {
            EasyExcel.write(response.getOutputStream(), dataClass)
                    .registerWriteHandler(new HorizontalCellStyleStrategy(getDefaultHeaderCellStyle(), getDefaultContentCellStyle()))
                    .registerWriteHandler(new DefaultColumnWidthStyleStrategy())
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("Sheet1")
                    .doWrite(data);
        } catch (IOException e) {
            log.info("写入数据发生异常", e);
            throw new BusinessException("写入数据失败");
        }
    }

    /**
     * 获取表头默认格式
     * @return
     */
    private static WriteCellStyle getDefaultHeaderCellStyle() {
        WriteCellStyle style = new WriteCellStyle();
        // 背景色
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteFont.setBold(true);
        style.setWriteFont(headWriteFont);
        // 水平居中
        style.setHorizontalAlignment(HorizontalAlignment.CENTER);
        return style;
    }

    /**
     * 获取内容默认格式
     * @return
     */
    private static WriteCellStyle getDefaultContentCellStyle() {
        WriteCellStyle style = new WriteCellStyle();
        // 单元格格式为"文本"
        style.setDataFormat((short) 49);
        // 字体
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 12);
        style.setWriteFont(contentWriteFont);
        // 自动换行
        style.setWrapped(true);
        // 垂直居中 style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 水平居中
        style.setHorizontalAlignment(HorizontalAlignment.CENTER);
        return style;
    }
}
